#!/bin/bash

source ../../version.pri

DIST=`lsb_release -s -i`
RELEASE=`lsb_release -s -r`
MACHINE=`uname -m`

TGZ_PREFIX=Drishti-$VERSION

rm -rf $TGZ_PREFIX

TGZ_BIN=$TGZ_PREFIX/bin
TGZ_LIB=$TGZ_PREFIX/lib
TGZ_DOC=$TGZ_PREFIX/doc
TGZ_PLUGINS=$TGZ_PREFIX/plugins
TGZ_TRANSLATIONS=$TGZ_PREFIX/translations

mkdir -p $TGZ_BIN
mkdir -p $TGZ_LIB
mkdir -p $TGZ_DOC
mkdir -p $TGZ_PLUGINS
mkdir -p $TGZ_TRANSLATIONS

#
# Drishti Binaries
# 
DRISHTI_BIN=../../bin
DRISHTI_EXE="drishti drishtiimport drishtilauncher drishtipaint"

for e in $DRISHTI_EXE; do
    echo $e
    cp $DRISHTI_BIN/$e $TGZ_BIN
    chrpath $TGZ_BIN/$e -r '$ORIGIN/../lib'
done

#
# Drishti Docs
#
cp -r ../../docs/* $TGZ_DOC

do_shared_lib () {
    LIBSRC=../../3rdParty/lib
    name=$1
    major=$2
    minor=$3
    build=$4

    lib=lib${name}.so.${major}.${minor}.${build}
    cp $LIBSRC/$lib $TGZ_LIB

    pushd $TGZ_LIB
    ln -s $lib lib${name}.so
    ln -s $lib lib${name}.so.${major}
    ln -s $lib lib${name}.so.${major}.${minor}
    popd
}

#
# Qt Libraries
#
QTLIBS="Qt3Support QtAssistantClient QtCLucene QtCore QtDesignerComponents QtDesigner QtGui QtHelp QtNetwork QtOpenGL QtScript QtScriptTools QtSql QtSvg QtTest QtWebKit QtXmlPatterns QtXml"
QTVERSION_MAJOR=4
QTVERSION_MINOR=5
QTVERSION_BUILD=3

for qt in $QTLIBS; do
    do_shared_lib $qt $QTVERSION_MAJOR $QTVERSION_MINOR $QTVERSION_BUILD
done

#
# Qt Binaries
#
QTBINS="assistant"

for qtb in $QTBINS; do
    cp ../../3rdParty/bin/$qtb $TGZ_BIN
    chrpath $TGZ_BIN/$qtb -r '$ORIGIN/../lib'
done

#
# Qt Plugins
#
QTPLUGINS="accessible codecs designer graphicssystems iconengines imageformats inputmethods sqldrivers"

for qtpd in $QTPLUGINS; do
    
    mkdir -p $TGZ_PLUGINS/$qtpd
    cp ../../3rdParty/plugins/$qtpd/*.so $TGZ_PLUGINS/$qtpd

    pushd $TGZ_PLUGINS/$qtpd
    for qtp in *.so; do
	chrpath $qtp -r '$ORIGIN/../../lib'
    done
    popd
done

#
# Qt Translations
#
cp ../../3rdParty/translations/*.qm $TGZ_TRANSLATIONS

#
# Qt Conf
#
QTCONF=$TGZ_BIN/qt.conf

echo "[Paths]" > $QTCONF
echo "Prefix=.." >> $QTCONF
echo "Plugins=plugins" >> $QTCONF
echo "Translations=translations" >> $QTCONF

#
# QGLViewer
#
do_shared_lib QGLViewer 2 3 2

#
# GLEW
#
do_shared_lib GLEW 1 5 1

#
# glmedia
#
do_shared_lib glmedia 0 0 0

#
# ffmpeg
#
do_shared_lib avcodec 52 32 0
do_shared_lib avutil 50 3 0
do_shared_lib avformat 52 37 0
do_shared_lib swscale 0 7 1

#
# ImageMagick
#
do_shared_lib Magick++ 2 0 0
do_shared_lib MagickCore 2 0 0
do_shared_lib MagickWand 2 0 0 

chrpath $TGZ_LIB/libMagick++.so -r '$ORIGIN'
chrpath $TGZ_LIB/libMagickCore.so -r '$ORIGIN'
chrpath $TGZ_LIB/libMagickWand.so -r '$ORIGIN'

#
# ImageMagick Plugins
#
IM_PLUGINS=ImageMagick-6.5.4
IM_MODULES=modules-Q16

IM_CODERS=$IM_PLUGINS/$IM_MODULES/coders
IM_FILTERS=$IM_PLUGINS/$IM_MODULES/filters

mkdir -p $TGZ_LIB/$IM_CODERS
cp ../../3rdParty/lib/$IM_CODERS/*.so $TGZ_LIB/$IM_CODERS
cp ../../3rdParty/lib/$IM_CODERS/*.la $TGZ_LIB/$IM_CODERS
pushd $TGZ_LIB/$IM_CODERS
for c in *.so; do
    chrpath $c -r '$ORIGIN/../../..'
done
popd

mkdir -p $TGZ_LIB/$IM_FILTERS
cp ../../3rdParty/lib/$IM_FILTERS/*.so $TGZ_LIB/$IM_FILTERS
cp ../../3rdParty/lib/$IM_FILTERS/*.la $TGZ_LIB/$IM_FILTERS
pushd $TGZ_LIB/$IM_FILTERS
for f in *.so; do
    chrpath $f -r '$ORIGIN/../../..'
done
popd

#
# PNG?
#
do_shared_lib png 3 39 0
do_shared_lib png12 0 39 0

#
# TIFF
#
do_shared_lib tiff 3 8 2
do_shared_lib tiffxx 3 8 2
 
#
# Make the tarball
#
rm -f ${TGZ_PREFIX}.tar.gz
tar -czf ${TGZ_PREFIX}-${DIST}-${RELEASE}-${MACHINE}.tar.gz ${TGZ_PREFIX}/*






